<layout name='Layout/ace_popup' />
<script type="text/javascript">
	function save() {
		$("#rc select option").each(function(i) {
			email = $(this).html().split(";")[1].split("&")[0];
			emp_no = $(this).val();
			name = jQuery.trim($(this).text()).replace(/<.*>/, '');
			html = conv_inputbox_item(emp_no, name,email, email);
			$("#recever .address_list", parent.document).append(html);
		});
		$("#cc select option").each(function(i) {
			email = ($(this).html().split(";")[1].split("&")[0]);
			emp_no = $(this).val();
			name = jQuery.trim($(this).text()).replace(/<.*>/, '');
			html_string = conv_inputbox_item(emp_no, name, email, email);
			$("#carbon_copy .address_list", parent.document).append(html_string);
		});
		$("#bcc select option").each(function(i) {
			email = ($(this).html().split(";")[1].split("&")[0]);
			emp_no = $(this).val();
			name = jQuery.trim($(this).text()).replace(/<.*>/, '');
			html_string = conv_inputbox_item(emp_no, name, email, email);
			$("#blind_carbon_copy .address_list", parent.document).append(html_string);
		});
		myclose();
	}

	// 显示AJAX 读取的数据
	function showdata(result){
		$("#addr_list").html("");
		if ( type = $("input[name='type']:checked").val() == "company") {
			var id = "dept_" + $("#company a.active").attr("node");
			var dept_name = $("#company a.active span").text();
			var email = "dept@group";
			var name = dept_name + "&lt;" + email + "&gt;";
			var html_string = conv_address_item(id, name);
			$("#addr_list").html(html_string);
		}
		for (s in result.data) {
			var id = result.data[s].id;
			var position_name = result.data[s].position_name;
			var emp_no = result.data[s].emp_no;
			var email = result.data[s].email;
			var name = result.data[s].name;
			var name = name + "/" + position_name + "&lt;" + email + "&gt;";
			var html_string = conv_address_item(id, name);
			$("#addr_list").append(html_string);
		}
	}


	$(document).ready(function() {
		$("#rb_{$type}").prop('checked', true);
		// 选择用户默认选择的类型
		$("#{$type}").removeClass("display-none");

		$("input[name='type']").on('click', function() {
			$("input[name='type']").each(function() {
				$("#" + $(this).val()).addClass("display-none");
			});
			$("#" + $(this).val()).removeClass("display-none")
		})

		$(".tree_menu  a").click(function() {
			$(".tree_menu a").removeClass("active");
			var type = $("input[name='type']:checked").val();
			$(this).addClass("active");
			sendAjax("{:U('read')}", "type=" + type + "&id=" + $(this).attr("node"), function(data) {
				showdata(data);
			})
			return false;
			//禁止连接生效
		});

		$(document).on("dblclick", "#addr_list label", function() {
			$text = $(this).text();
			$val = $(this).find("input").val();
			if ($("#rc select option[value='" + $val + "']").val() == undefined) {
				$("<option></option>").val($val).text($text).appendTo("#rc select");
				$("#rc_count").text("(" + $("#rc select option").length + ")");
			};
		});

		$("#rc  select").on("dblclick", function() {
			$(this).find("option:selected").remove();
			$("#rc_count").text("(" + $("#rc select option").length + ")");
		});

		$("#cc  select").on("dblclick", function() {
			$(this).find("option:selected").remove();
			$("#cc_count").text("(" + $("#cc select option").length + ")");
		});

		$("#bcc  select").on("dblclick", function() {
			$(this).find("option:selected").remove();
			$("#bcc_count").text("(" + $("#bcc select option").length + ")");
		});

		$("#addr_list").on("mouseover", function() {
			$("#addr_list label").draggable({
				appendTo : "body",
				helper : "clone"
			});
		});

		$("#rc select").droppable({
			activeClass : "ui-state-default",
			hoverClass : "ui-state-hover",
			accept : ":not(.ui-sortable-helper)",
			drop : function(event, ui) {
				$text = ui.draggable.text();
				$val = ui.draggable.find("input").val();
				if ($("#rc select option[value='" + $val + "']").val() == undefined) {
					$("<option></option>").val($val).text($text).appendTo(this);
					$("#rc_count").text("(" + $("#rc select option").length + ")");
				};
			},
		}).sortable({
			items : "option:not(.placeholder)",
			sort : function() {
				$(this).removeClass("ui-state-default");
			}
		});
		$("#cc select").droppable({
			activeClass : "ui-state-default",
			hoverClass : "ui-state-hover",
			accept : ":not(.ui-sortable-helper)",
			drop : function(event, ui) {
				$text = ui.draggable.text();
				$val = ui.draggable.find("input").val();
				if ($("#cc select option[value='" + $val + "']").val() == undefined) {
					$("<option></option>").val($val).text($text).appendTo(this);
					$("#cc_count").text("(" + $("#cc select option").length + ")");
				};
			},
		}).sortable({
			items : "li:not(.placeholder)",
			sort : function() {
				$(this).removeClass("ui-state-default");
			}
		});
		$("#bcc select").droppable({
			activeClass : "ui-state-default",
			hoverClass : "ui-state-hover",
			accept : ":not(.ui-sortable-helper)",
			drop : function(event, ui) {
				$text = ui.draggable.text();
				$val = ui.draggable.find("input").val();
				if ($("#bcc select option[value='" + $val + "']").val() == undefined) {
					$("<option></option>").val($val).text($text).appendTo(this);
					$("#bcc_count").text("(" + $("#bcc select option").length + ")");
				};
			},
		}).sortable({
			items : "li:not(.placeholder)",
			sort : function() {
				$(this).removeClass("ui-state-default");
			}
		});
	})
	//最终确认

	function add_address(name) {
		$("input:checked[name='addr_id']").each(function() {
			$text = $(this).parents("label").find("span").text();
			$val = $(this).val();
			if ($("#" + name + " select option[value='" + $val + "']").val() == undefined) {
				$("<option></option>").val($val).text($text).appendTo("#" + name + " select");
				$("#" + name + "_count").text("(" + $("#" + name + " select option").length + ")");
			};
		})
	}

	//-->
</script>
<div class="panel panel-default ">
	<div class="panel-heading clearfix">
		<div class="pull-left">
			<label>
				<input class="ace"  type="radio" id="rb_company" name="type" value="company" >
				<span class="lbl">公司</span> </label>

			<label>
				<input  class="ace"  type="radio" id="rb_rank" name="type" value="rank">
				<span class="lbl">职级</span> </label>

			<label>
				<input  class="ace"  type="radio" id="rb_position" name="type" value="position">
				<span class="lbl">职位</span> </label>

		</div>
		<div class="pull-right">
			<a  onclick="save();" class="btn btn-sm btn-primary">确定</a>
			<a  onclick="myclose();" class="btn btn-sm btn-primary">关闭</a>
		</div>
	</div>
	<div class="panel-body">
		<div class="col-28 pull-left">
			<div class="">
				<b>地址簿</b>
			</div>
			<div class="popup_tree_menu" >
				<div id="company" class="display-none" style="height:200px;">
					{$list_company}
				</div>
				<div id="rank" class="display-none" style="height:200px;">
					{$list_rank}
				</div>
				<div id="position" class="display-none" style="height:200px;">
					{$list_position}
				</div>
			</div>
			<div>
				<div id="addr_list" style="width:100%;height:210px;"></div>
			</div>
		</div>
		<div class="col-34 pull-right">
			<div>
				<b style="padding-left:60px;">接收人</b><span id="rc_count"></span>
			</div>
			<div class="clearfix" style="margin-bottom: 15px;">
				<label class="col-4 pull-left text-right" ><a onclick="add_address('rc');" class="btn btn-sm btn-primary"> <i class="fa fa-angle-double-right"></i> </a> </label>
				<div class="col-28 pull-right">
					<div id="rc" style="width:100%;height:424px;overflow:hidden">
						<select size="6" style="height:100%;width:100%;"></select>
					</div>
				</div>
			</div>
			
		</div>
	</div>
</div>